import { message } from 'antd';
import {
    gettemplatelist,
    detailworkflowtemplate,
    getProcessJson,
    getFromJson
} from '@/services/flowManage/templateMange';

// const flowJsonMock = require('../FlowManage/TemplateManage/mocks/xiamenqingjia.json');

export default {
    namespace: 'templateManage',
    state: {
        // 流程模板列表
        templateList: [],
        // 流程基本设置详情
        templateDetail: {},
        flowJson: {},
        flowForm: {}
    },
    effects: {
        *fetchList({ payload }, { call, put }) {
            const result = yield call(gettemplatelist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        templateList: result.data
                    }
                });
            }
        },
        *fetchTemplateDetail({ payload, callback }, { call, put }) {
            const result = yield call(detailworkflowtemplate, payload);
            if (result && result.code === 10000) {
                callback(result.data);
                yield put({
                    type: 'save',
                    payload: {
                        templateDetail: result.data
                    }
                });
            }
        },
        *fetchFlowJson({ payload }, { call, put }) {
            const result = yield call(getProcessJson, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        flowJson: result.data
                        // flowJson: flowJsonMock
                    }
                });
            } else {
                message.error(result.msg);
            }
        },
        *fetchFlowForm({ payload }, { call, put }) {
            const result = yield call(getFromJson, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        flowForm: result.data
                    }
                });
            } else {
                message.error(result.msg);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear(state) {
            return {
                ...state,
                // 流程模板列表
                templateList: [],
                // 流程基本设置详情
                templateDetail: {},
                flowJson: {},
                flowForm: {}
            };
        }
    }
};
